package com.jt;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializationContext;
import org.springframework.data.redis.serializer.RedisSerializer;

//重构caheManager对象，其目的是改变AOP方式应用redis的序列化和反序列化的方式
@Configuration
public class CachingConfig /*extends CachingConfigurerSupport*/ {

    @Autowired
    private RedisConnectionFactory redisConnectionFactory;

    @Bean
    public CacheManager redisCacheManager() {
        //定义RedisCache配置对象
        RedisCacheConfiguration cacheConfig=
                RedisCacheConfiguration.defaultCacheConfig()
        //定义key的序列化方式
        .serializeKeysWith(
                RedisSerializationContext.
                        SerializationPair.fromSerializer(RedisSerializer.string()))
        //定义value的序列化方式
        .serializeValuesWith(
                RedisSerializationContext.SerializationPair
                        .fromSerializer(RedisSerializer.json()));

        return  RedisCacheManager.builder(redisConnectionFactory)
                .cacheDefaults(cacheConfig)
                .transactionAware()
                .build();
    }
}